// pages/shop/search/search.js
const app = getApp()
var xrsdk = require('../../../vendor/hope-sdk/core/request.js');
var config = require('../../../config.js');
const util = require('../../../utils/util.js');
const QQMapWX = require('../../../libs/qqmap-wx-jssdk.min.js');
let qqmapsdk;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    code:'',
    latitude: '',
    longitude: '',
    province: '',
    city: '',
    shopName: '',
    shopList: [],
    pageIndex: 1,
    loadding: false,
    pullUpOn: true,
    shop_id: 0,
    amapPlugin: null,
    //key: 'W4WBZ-TUD65-IDAIR-QPM36-HMFQ5-CGBZP',
    key:'6KMBZ-65KR3-RWN3M-Y3MIB-4OFGF-HDFK6',
    modal9: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    qqmapsdk = new QQMapWX({
      key: that.data.key
    });
    that.currentLocation();
  },
  currentLocation: function() {
    //当前位置
    const that = this;
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        that.getAddress(res.longitude, res.latitude);
      }
    })
  },
  getAddress: function(lng, lat) {
    var that = this;
    //根据经纬度获取地址信息
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: lat,
        longitude: lng
      },
      success: (res) => {
        console.log(res)
        that.setData({
          province: res.result.ad_info.province,
          city: res.result.ad_info.city,
          address: res.result.address, //res.result.address
        })
        that.initShop();
      },
      fail: (res) => {
        this.setData({
          address: "获取位置信息失败"
        })
      }
    })
  },
  initShop: function() {
    var that = this;
    xrsdk.request(app, {
      url: config.service.shopList,
      login: false,
      data: {
        latitude: that.data.latitude,
        longitude: that.data.longitude,
        province: that.data.province,
        city: that.data.city,
        keyword: that.data.shopName,
        page: that.data.pageIndex,
        page_size: 10
      },
      success: function(res) {
        if (res.data.data.has_more == false) {
          that.setData({
            pullUpOn: false
          })
        }
        that.setData({
          shopList: that.data.shopList.concat(res.data.data.data),
          pageIndex: that.data.pageIndex + 1,
          loadding: false
        })
        wx.stopPullDownRefresh()
      },
      fail: function(res) {},
    })
  },
  initData: function() {
    this.setData({
      shopList: [],
      pageIndex: 1,
      pullUpOn: true,
      loadding: true,
      shop_id: 0
    })
  },
  shopNameInput: function(e) {
    this.setData({
      shopName: e.detail.value
    });
    console.log(e);
  },
  shopNameConfirm: function(e) {
    console.log(e);
    var that = this;
    this.initData();
    this.initShop();
  },
  btnPay: function(e) {
    var that = this;
    if (that.data.shop_id == 0 || that.data.shop_id < 1) {
      util.toast('请选择门店');
      return;
    }
    var row = that.data.shopList.find(that.getShopItem);
    if (row == null || row == undefined) {
      util.toast('请选择门店');
      return;
    }
    //console.log(row);
    if (row.service_range == 1) {
      that.show9();
    } else {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2]; //上一个页面
      prevPage.setData({
        shop: row
      }, function() {
        wx.navigateBack({
          delta: 1
        });
      });
    }
  },
  getShopItem: function(element) {
    var that = this;
    return element.id == that.data.shop_id;
  },
  selectRadio: function(e) {
    console.log(e);
    this.setData({
      shop_id: e.detail.value
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.initData();
    this.initShop();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (!this.data.pullUpOn) return;
    this.setData({
      loadding: true
    }, () => {
      this.initShop();
    })
  },
  show9() {
    this.setData({
      modal9: true
    })
  },
  hide9() {
    this.setData({
      modal9: false
    })
  },
  validate() {
    var that = this;
    var code = that.data.code;
    var row = that.data.shopList.find(that.getShopItem);
    if (row == null || row == undefined) {
      util.toast('请选择门店');
      return;
    }
    wx.showLoading({
      title: '处理中...',
      mask: true
    })
    xrsdk.request(app, {
      url: config.service.check_trade,
      login: true,
      data: {
        code: code
      },
      success: function (res) {
        wx.hideLoading();
        console.log(res);
        if(res.data.data){
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2]; //上一个页面
          prevPage.setData({
            shop: row
          }, function () {
            wx.navigateBack({
              delta: 1
            });
          });
          that.hide9();
        }else{
          util.toast('单号不存在');
          return;
        }
      },
      fail: function (res) { },
      authfail: function (e) {
        wx.navigateTo({
          url: "../../auth/auth"
        })
      }
    })
  },
  codeInput: function (e) {
    console.log(e.detail.value);
    this.setData({
      code: e.detail.value
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})